<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="校区" prop="campus" label-width="80">
        <el-select v-model="queryParams.campus" style="width: 180px" placeholder="请选择校区" clearable>
          <el-option
            v-for="dict in dict.type.sys_campus_choose"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="学科" prop="subject">
        <el-select v-model="queryParams.subject" style="width: 180px" placeholder="请选择学科" clearable>
          <el-option
            v-for="dict in dict.type.sys_teaching_subject"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" style="width: 180px" placeholder="请选择老师类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_teacher_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="班次" prop="shiftId">
        <el-select v-model="queryParams.shiftId" style="width: 180px" placeholder="请选择" clearable>
          <el-option
            v-for="item in classesList"
            :key="item.class_id"
            :label="item.name"
            :value="item.class_id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="老师" prop="teacherName" label-width="30">
        <el-select v-model="queryParams.teacherName" style="width: 180px" placeholder="请选择" clearable>
          <el-option
            v-for="(item,index) in teacherNameList"
            :key="index"
            :label="item"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div>
      <el-calendar>
        <!-- 这里使用的是 2.5 slot 语法，对于新项目请使用 2.6 slot 语法-->
        <template
          slot="dateCell"
          slot-scope="{date, data}">
          <p :class="data.isSelected ? 'is-selected' : ''">
            {{ data.day.split('-').slice(1).join('-') }}
            <el-button
              type="success"
              size="mini"
              @click="handleRostering(data.day)"
              v-hasPermi="['teacher:rostering:rostering']"
            >排班</el-button>
            <br>
            <el-tag
              v-for="content in dealMyDate(data)"
            >{{content}}</el-tag>
          </p>
        </template>
      </el-calendar>
    </div>


    <pagination
      v-show="total>0"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改老师排班对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="考勤组ID" prop="groupId">
          <el-input v-model="form.groupId" placeholder="请输入考勤组ID" />
        </el-form-item>
        <el-form-item label="用户userId" prop="userid">
          <el-input v-model="form.userid" placeholder="请输入用户userId" />
        </el-form-item>
        <el-form-item label="工作日，代表具体哪一天的排班" prop="workDate">
          <el-date-picker clearable
            v-model="form.workDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择工作日，代表具体哪一天的排班">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划打卡时间" prop="planCheckTime">
          <el-date-picker clearable
            v-model="form.planCheckTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划打卡时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 新增排班 -->
    <el-dialog title="新增排班" :visible.sync="rosteringFormOpen" width="500px" append-to-body>
      <el-form ref="form" :model="rosteringForm" :rules="rules" label-width="80px">
<!--        <el-form-item label="时间" prop="time">-->
<!--          <el-input v-model="rosteringForm.time" disabled />-->
<!--        </el-form-item>-->
        <el-form-item label="考勤组">
          <el-select v-model="rosteringForm.group_id" placeholder="请选择">
            <el-option
              v-for="item in groupAndClassesList"
              :key="item.group_id"
              :label="item.name"
              :value="item.group_id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="班次选择">
          <el-select v-model="rosteringForm.shift_id" placeholder="请选择">
            <el-option
              v-for="item in classesList"
              :key="item.shift_id"
              :label="item.name"
              :value="item.shift_id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRosteringForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRostering, getRostering, delRostering, addRostering, updateRostering,getUserGroupInfo,teacherRostering,rosteringList,getTeacherNameList } from "@/api/teacher/rostering";
export default {
  dicts: ['sys_teaching_subject','sys_campus_choose','sys_teacher_type'],
  name: "Rostering",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 老师排班表格数据
      rosteringList: [],
      classesList:[],
      groupAndClassesList: [],
      teacherNameList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      rosteringFormOpen: false,
      // 查询参数
      queryParams: {
        checkType: null,
        groupId: null,
        userid: null,
        workDate: null,
        planCheckTime: null
      },
      // 表单参数
      form: {},
      rosteringForm: {
        group_id: "",
        shift_id: "",
        time: ""
      },
      // 表单校验
      rules: {
      },
      resDate: [
        // {"date":"2022-12-20","content":["放假","打豆豆"]},
        // {"date":"2022-12-26","content":["去交电费"]},
        // {"date":"2022-11-26","content":["去学习vue"]}
      ]
    };
  },
  created() {
    this.getList();
    this.getGroupInfo();
    this.getTeacherName();
  },
  methods: {
    /** 查询老师排班列表 */
    getList() {
      this.loading = true;
      listRostering(this.queryParams).then(response => {
        this.resDate = response
        this.loading = false;
      });
    },
    /** 查询考勤组和班次信息 */
    getGroupInfo() {
      getUserGroupInfo().then(response => {
        this.groupAndClassesList = response;
        this.classesList = this.groupAndClassesList[0].classes;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.rosteringFormOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        checkType: null,
        groupId: null,
        userid: null,
        workDate: null,
        planCheckTime: null
      };
      this.resetForm("form");
    },
    test(date) {
      console.log(date)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加老师排班";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRostering(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改老师排班";
      });
    },
    dealMyDate(date) {
      let len = this.resDate.length
      let res = ""
      for(let i=0; i<len; i++){
        if(this.resDate[i].date == date.day) {
          res = this.resDate[i].content
          break
        }
      }
      return res
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRostering(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRostering(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitRosteringForm() {
      console.log(this.rosteringForm);
      teacherRostering(this.rosteringForm).then(response => {
        this.$modal.msgSuccess(response.msg);
        this.rosteringFormOpen = false;
        this.rosteringForm = {};
        this.getList();
      });
    },
    getTeacherName() {
      getTeacherNameList().then(response => {
        this.teacherNameList = response;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除老师排班编号为"' + ids + '"的数据项？').then(function() {
        return delRostering(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 排班 */
    handleRostering(day) {
      this.resetForm("rosteringForm");
      this.rosteringFormOpen = true;
      this.rosteringForm.time = day;
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('teacher/rostering/export', {
        ...this.queryParams
      }, `rostering_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
  .is-selected {
    color: #1989FA;
  }
</style>
